#include "gofgame/dungeonkeeper/configuration/LevelParser.h"
#include "cocos2d.h"
#include "string.h"
#include "stdio.h"

using namespace gofgame::dungeonkeeper::configuration;
USING_NS_CC;

static LevelParser *s_SharedeLevelParser = NULL;

LevelParser::LevelParser() {
	
}

LevelParser * LevelParser::getSharedLevelParser(){
	if(!s_SharedeLevelParser) {
		s_SharedeLevelParser = new LevelParser();
	}
	return s_SharedeLevelParser;
}

LevelConf* LevelParser::parse(char *file){
	CCSAXParser *xmlParser = new CCSAXParser();
	this->tempLevel = new LevelConf();
	xmlParser->setDelegator(this);
	xmlParser->parse(file);
	return tempLevel;
}

void LevelParser::startElement(void *ctx, const char *name, const char **atts){
	if(strcmp(name,"gg:level") == 0 ) {
		for (int i = 0; atts[i]; i += 2) {
			if (strcmp(atts[i], "code") == 0) {
				tempLevel->setCode(atts[i + 1]);
			} else if (strcmp(atts[i], "name") == 0) {
				tempLevel->setName(const_cast<char *>(atts[i + 1]));
			} else if (strcmp(atts[i], "background-image") == 0) {
				tempLevel->setBackgroundImage(const_cast<char *>(atts[i + 1]));
			} else if (strcmp(atts[i],"background-sound") == 0 ) {
				tempLevel->setBackgroundSound(const_cast<char *>(atts[i + 1]));
			}
		}
	}
	if(strcmp(name,"gg:base") == 0){
		BaseConf *baseConf = new BaseConf();
		for(int i = 0;atts[i];i +=2) {
			if(strcmp(atts[i],"id") == 0) {
				baseConf->setId(atoi(atts[i+1]));
			}
			if(strcmp(atts[i],"lv") == 0){
				baseConf->setLv(atoi(atts[i+1]));
			}
		}
		tempLevel->baseConf = baseConf;
	}
	if(strcmp(name,"gg:unit") == 0){
		tempUnitConf = new UnitConf();
		unitFlag = true;
		for(int i = 0;atts[i];i +=2) {
			if(strcmp(atts[i],"id") == 0) {
				tempUnitConf->setId(atoi(atts[i+1]));
			}
			if(strcmp(atts[i],"lv") == 0){
				tempUnitConf->setLv(atoi(atts[i+1]));
			}
		}
	}
	if(strcmp(name,"gg:item") == 0) {
		ItemConf *itemConf = new ItemConf();
		for(int i = 0;atts[i];i +=2) {
			if(strcmp(atts[i],"id") == 0) {
				itemConf->setId(atoi(atts[i+1]));
			}
			if(strcmp(atts[i],"weight") == 0){
				itemConf->setWeight(atof(atts[i+1]));
			}
		}
		if(unitFlag) {
			tempUnitConf->items.push_back(itemConf);
		}
	}
	if(strcmp(name,"gg:period") == 0){
		tempPeriodConf = new PeriodConf();
		periodFlag = true;
		for(int i = 0;atts[i];i +=2) {
			if(strcmp(atts[i],"start-time") == 0) {
				tempPeriodConf->setStartTime(atoi(atts[i+1]));
			}
			if(strcmp(atts[i],"end-time") == 0){
				tempPeriodConf->setEndTime(atoi(atts[i+1]));
			}
		}
	}
	if(strcmp(name,"gg:create-unit") == 0) {
		CreateUnitConf *createConf = new CreateUnitConf();
		for(int i = 0;atts[i];i +=2) {
			if(strcmp(atts[i],"id") == 0) {
				createConf->setId(atoi(atts[i+1]));
			}
			if(strcmp(atts[i],"lv") == 0){
				createConf->setLv(atoi(atts[i+1]));
			}
			if(strcmp(atts[i],"cooldown") == 0){
				createConf->setCoolDown(atof(atts[i+1]));
			}
		}
		createConf->dTime = 0;
		if(periodFlag) {
			tempPeriodConf->createUnitConfs.push_back(createConf);
		}
	}
};
void LevelParser::endElement(void *ctx, const char *name){
	if(strcmp(name,"gg:unit") == 0){
		tempLevel->unitConfs.push_back(tempUnitConf);
		tempUnitConf = NULL;
		unitFlag = false;
	}
	if(strcmp(name,"gg:period") == 0){
		tempLevel->periodConfs.push_back(tempPeriodConf);
		tempPeriodConf = NULL;
		periodFlag = false;
	}

};
void LevelParser::textHandler(void *ctx, const char *s, int len){
	CCLOG("MIAO ");
};